home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / resource.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  2.1 KB  |  50 lines

  1. from xpcom import components
  2. import os, sys
  3. import re
  4.  
  5. # Strategy: ask the directory service for
  6. # NS_XPCOM_CURRENT_PROCESS_DIR, the directory "associated with this
  7. # process," which is read to mean the root of the Mozilla
  8. # installation. Use a fixed offset from this path.
  9.  
  10. # Another copy appears in components/pybridge.py, in the bootstrap
  11. # code; if you change this function, change that one too.
  12. def appRoot():
  13.     # This reports the path to xulrunner.exe -- admittedly a little
  14.     # misleading. In general, this will be in a 'xulrunner'
  15.     # subdirectory underneath the actual application root directory.
  16.     klass = components.classes["@mozilla.org/file/directory_service;1"]
  17.     service = klass.getService(components.interfaces.nsIProperties)
  18.     file = service.get("XCurProcD", components.interfaces.nsIFile)
  19.     return file.path
  20.  
  21. def resourceRoot():
  22.     if 'RUNXUL_RESOURCES' in os.environ:
  23.     # We're being run it test mode by our 'runxul' distutils
  24.     # command.
  25.     return os.environ['RUNXUL_RESOURCES']
  26. #    return os.path.join(appRoot(), 'resources') # NEEDS XXX TEST
  27.     return os.path.join(appRoot(), '..', 'resources') # NEEDS XXX TEST
  28.  
  29. # Note: some of these functions are probably not absolutely correct in
  30. # the face of funny characters in the input paths. In particular,
  31. # url() doesn't DTRT when the path contains spaces. But they should be
  32. # sufficient for resolving resources, since we have control over the
  33. # filenames.
  34.  
  35. # Find the full path to a resource data file. 'relative_path' is
  36. # expected to be supplied in Unix format, with forward-slashes as
  37. # separators. The output, though, uses the native platform separator.
  38. def path(relative_path):
  39.     rootParts = re.split(r'\\', resourceRoot())
  40.     myParts = re.split(r'/', relative_path)
  41.     return '\\'.join(rootParts + myParts)
  42.  
  43. # As path(), but return a URL that will retrieve the resource
  44. # instead. (We end up returning a relative URL that will work on the
  45. # webserver we serve on 127.0.0.1. That way, loading stylesheets from
  46. # a resource URL will be legal under the Mozilla "same origin"
  47. # policy.)
  48. def url(relative_path):
  49.     return "/dtv/resource/" + relative_path
  50.